import { defineStore } from 'pinia'
import { ref } from 'vue'
import { reqLogin, reqUserInfo } from '@/apis/user'
import type { UserInfo, UserResponseData } from '@/apis/user/type'
// 路由约束
import type { RouteRecordRaw } from 'vue-router'
import { constantRoutes } from '@/router/routes'
export const useUserStore = defineStore(
  'admin-user',
  () => {
    const userInfo = ref<UserInfo>({
      userId: '',
      avatar: '',
      username: '',
      passwoed: '',
      desc: '',
      roles: [],
      buttons: [],
      routes: [],
      token: ''
    })
    const token = ref('')
    // 路由规则
    const getUserInfo = async () => {
      const res: UserResponseData = await reqUserInfo()
      console.log(res)
    }
    const menuRoutes: RouteRecordRaw[] = constantRoutes
    const goLogin = async (data: any) => {
      const res: any = await reqLogin(data)
      token.value = res.data
    }
    return {
      userInfo,
      token,
      goLogin,
      menuRoutes,
      getUserInfo
    }
  },
  {
    persist: true
  }
)
